package org.jeecg.modules.front.interceptor;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.Serializable;

//import com.iwhalecloud.craftsman.oms.consts.OmsConst;

@Slf4j
public class MemberContext implements Serializable {

    private static ThreadLocal<String> memberIdThreadLocal = new ThreadLocal<>();

    private static ThreadLocal<String> userSessionIdThreadLocal = new ThreadLocal<>();


    public static String getMemberId() {
        return memberIdThreadLocal.get();
    }

    public static void setMemberId(String memberId) {
        memberIdThreadLocal.set(memberId);
    }

    public static String getUserSessionId() {
        return userSessionIdThreadLocal.get();
    }

    public static void setUserSessionId(String userSessionId) {
        userSessionIdThreadLocal.set(userSessionId);
    }

    public static void removeMemberSession() {
        memberIdThreadLocal.remove();
        userSessionIdThreadLocal.remove();
    }

    /**
     * 获取当前servlet的request对象，不在servlet环境下返回null
     *
     * @return
     * @author Ji.kai 2018-11-5
     */
    public static final HttpServletRequest getRequest() {
        ServletRequestAttributes attrs = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (attrs == null) {
            return null;
        }
        HttpServletRequest request = attrs.getRequest();
        return request;
    }

    /**
     * 获取当前servlet的request对象，不在servlet环境下返回null.
     *
     * @return
     * @author Ji.kai 2018-11-5
     */
    public static final HttpSession getSession() {
        HttpServletRequest req = getRequest();
        if (req == null) {
            return null;
        }
        HttpSession session = req.getSession();
        return session;
    }


}
